home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / games / seahaven / score.c++ < prev    next >
C/C++ Source or Header  |  1994-08-01  |  5KB  |  180 lines

  1. /*
  2.  * Copyright 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  * Author:  Terry Weissman
  19.  *          weissman@sgi.com
  20.  */
  21.  
  22. #include "seahaven.h"
  23.  
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <stdlib.h>        // For getenv()
  27.  
  28. ScoreRec::ScoreRec() {
  29.     int x = 10;
  30.     int y = GAMEHEIGHT - 20 - 5 * (font->ascent + font->descent);
  31.     XSetWindowAttributes attributes;
  32.     long valuemask = CWEventMask | CWBackPixel | CWWinGravity;
  33.     attributes.event_mask = ExposureMask;
  34.     attributes.background_pixel = GetColor("forestgreen", "grey80",
  35.                        WhitePixel(dpy, screen));
  36.     attributes.win_gravity = SouthWestGravity;
  37.     window = XCreateWindow(dpy, toplevel, x, y,
  38.                GAMEWIDTH, GAMEHEIGHT, 0, (int) CopyFromParent,
  39.                InputOutput, (Visual *) CopyFromParent,
  40.                valuemask, &attributes);
  41.     XLowerWindow(dpy, window);
  42.     XMapWindow(dpy, window);
  43.  
  44.  
  45.     sprintf(savefile, "%s/.seahavensave", getenv("HOME"));
  46.     FILE *fid = fopen(savefile, "r");
  47.     if (fid) {
  48.     fscanf(fid, "%d %d %d %d %d %d\n", &wins, &losses, &streak,
  49.            &maxwinstreak, &maxlosestreak, &wonlast);
  50.     for (int i=0 ; i<52 ; i++) {
  51.         int suit, value;
  52.         fscanf(fid, "%d %d\n", &suit, &value);
  53.         deck[i] = cards[suit][value];
  54.     }
  55.     fclose(fid);
  56.     LoadStacks(deck);
  57.     } else {
  58.     wins = -1;
  59.     streak = -1;
  60.     losses = 0;
  61.     maxwinstreak = 0;
  62.     maxlosestreak = 0;
  63.     wonlast = True;
  64.     }
  65.     gc = XCreateGC(dpy, window, 0, NULL);
  66.     XSetForeground(dpy, gc, GetColor("black", "white", BlackPixel(dpy, screen)));
  67.     XSetFont(dpy, gc, font->fid);
  68.     woncurgame = lostcurgame = False;
  69.     message = "";
  70. }
  71.  
  72.  
  73.  
  74. void ScoreRec::printLine(char *str, int value, char *plural) {
  75.     char buf[200];
  76.     cury += font->ascent + font->descent;
  77.     sprintf(buf, str, value, value == 1 ? "" : plural);
  78.     if (buf[0]) XDrawString(dpy, window, gc, curx, cury, buf, strlen(buf));
  79. }
  80.  
  81.  
  82.  
  83. void ScoreRec::repaint(Bool erasefirst) {
  84.     curx = 0;
  85.     cury = 0;
  86.     if (erasefirst) XClearWindow(dpy, window);
  87.     printLine("%5d win%s", wins);
  88.     printLine("%5d loss%s", losses, "es");
  89.     printLine("%5d game%s played", wins + losses);
  90.     curx = GAMEWIDTH / 2;
  91.     cury = 0;
  92.     if (streak > 0) {
  93.     if (wonlast) printLine("%5d game winning streak", streak);
  94.     else printLine("%5d game losing streak", streak);
  95.     }
  96.     if (maxwinstreak > 0) {
  97.     printLine("%5d game%s in longest winning streak", maxwinstreak);
  98.     }
  99.     if (maxlosestreak > 0) {
  100.     printLine("%5d game%s in longest losing streak", maxlosestreak);
  101.     }
  102.     curx /= 2;
  103.     printLine(message);
  104. }
  105.  
  106.  
  107. void ScoreRec::wonGame() {
  108.     if (!woncurgame && !lostcurgame) {
  109.     woncurgame = True;
  110.     wins++;
  111.     if (!wonlast) streak = 0;
  112.     streak++;
  113.     if (maxwinstreak < streak) maxwinstreak = streak;
  114.     wonlast = True;
  115.     message = "";
  116.     repaint(True);
  117.     }
  118. }
  119.  
  120.  
  121.  
  122. void ScoreRec::lostGame() {
  123.     if (!woncurgame && !lostcurgame) {
  124.     lostcurgame = True;
  125.     losses++;
  126.     if (wonlast) streak = 0;
  127.     streak++;
  128.     if (maxlosestreak < streak) maxlosestreak = streak;
  129.     wonlast = False;
  130.     message = "";
  131.     repaint(True);
  132.     }
  133. }
  134.  
  135.  
  136. void ScoreRec::newGame() {
  137.     woncurgame = lostcurgame = False;
  138.     setMessage("");
  139. }
  140.  
  141.  
  142. Bool ScoreRec::getGameWonOrLost() {
  143.     return woncurgame || lostcurgame;
  144. }
  145.  
  146.  
  147. void ScoreRec::saveGameBegin() {
  148.     curx = 0;
  149. }
  150.  
  151. void ScoreRec::saveGameCard(Card c) {
  152.     deck[curx++] = c;
  153. }
  154.  
  155. void ScoreRec::saveGameEnd() {
  156.     if (curx != 52) Punt("Bug in ScoreRec::saveGameEnd!");
  157.     FILE *fid = fopen(savefile, "w");
  158.     if (!fid) Punt("Can't open file in ScoreRec::saveGameEnd!");
  159.     fprintf(fid, "%d %d %d %d %d %d\n", wins, losses, streak,
  160.         maxwinstreak, maxlosestreak, wonlast);
  161.     for (int i=0 ; i<52 ; i++) {
  162.     fprintf(fid, "%d %d\n", deck[i]->getSuit(), deck[i]->getValue());
  163.     }
  164.     fclose(fid);
  165. }
  166.  
  167.  
  168. void ScoreRec::setMessage(char *msg) {
  169.     if (strcmp(message, msg)) {
  170.     char *oldmsg = message;
  171.     message = msg;
  172.     repaint(*oldmsg != 0);
  173.     }
  174. }
  175.  
  176.  
  177. Bool ScoreRec::getNeedStartingGame() {
  178.     return wins < 0;
  179. }
  180.